// 发送送消息给content
function sendMessageToContentScript(message, callback) {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
            if (callback) callback(response);
        });
    });
}

// 登录
$('#login').click(function () {
    sendMyMsg('login');
});

// 开始
$('#start').click(function () {
    sendMyMsg('start');
});

// 停止
$('#stop').click(function () {
    sendMyMsg('stop');
});

function sendMyMsg(cmd) {
    sendMessageToContentScript({ cmd: cmd }, function (response) {
        // 发送chrome通知
        chrome.notifications.create(null, {
            type: 'basic',
            iconUrl: 'img/logo.png',
            title: '操作通知',
            message: response.message + '\n' + (new Date).toLocaleString()
        });
    });
}

// 读取采购网址
chrome.storage.local.get({ myUrl: '' }, function (items) {
    $('#current-url').val(items.myUrl);
});

// 读取采购标签
chrome.storage.local.get({ myGoodsTagKey: 0 }, function (items) {
    $('#current-goods-tag-key').val(items.myGoodsTagKey);
});

// 更新数据
$('#update-url').click(function () {
    chrome.storage.local.set({ myUrl: $('#current-url').val() });
    chrome.storage.local.set({ myGoodsTagKey: $('#current-goods-tag-key').val() });

    // 发送chrome通知
    chrome.notifications.create(null, {
        type: 'basic',
        iconUrl: 'img/logo.png',
        title: '操作通知',
        message: '更新配置成功' + '\n' + (new Date).toLocaleString()
    });
});
